##  ---------------------------------------------------------------------------------------------  ##
##  ---------------------------------------------------------------------------------------------  ##
##  ---  second article of the thesis - extensive margin of international trade for Mercosur  ---  ##
##  ---                      by Erik Figueiredo and Alexandre Loures                          ---  ##
##  ---                      Joao Pessoa, Paraiba, Brazil 2015/09/23                          ---  ##
##  ---------------------------------------------------------------------------------------------  ##
##  ---------------------------------------------------------------------------------------------  ##

## Comments:
## ido - refers to the column with the ISO codes of each origin country
## idd - refers to the column with the ISO codes of each destination country

## Article available in: https://www.researchgate.net/publication/305485838
## Code Application (R) available in: https://www.researchgate.net/publication/320853517
## Code Application (Stata) available in: https://www.researchgate.net/publication/320840746

##  -----------------------------  ##
##  downloading package and basis  ##
##  -----------------------------  ##

library (foreign)

mydata <- read.dta (choose.files())

##  ----------------------  ##
##  creating variable year  ##
##  ----------------------  ##

y <- table (mydata$year)

ye <- as.data.frame (y)

year <- as.character (ye$Var1)

##  ---------------------  ##
##  creating variable ido  ##
##  ---------------------  ##

i <- table (mydata$ido)

id <- as.data.frame (i)

ido <- as.character (id$Var1)

##  ---------------------  ##
##  creating variable idd  ##
##  ---------------------  ##

i_d <- table (mydata$idd)

id_d <- as.data.frame (i_d)

idd <- as.character (id_d$Var1)

##  ---------------------  ##
##  creating variable HS4  ##
##  ---------------------  ##

H <- table (mydata$HS4)

HS <- as.data.frame (H)

HS4 <- as.character (HS$Var1)

##  -------------------  ##
##  expanding the basis  ##
##  -------------------  ##

expand <- expand.grid (year, ido, idd, HS4)

colnames (expand)[1:4] <- c('year', 'ido', 'idd', 'HS4')

##  ------------------------------------------------------------------------  ##
##  dropping equals observations (when the levels of the variable are equal)  ##
##  ------------------------------------------------------------------------  ##

expand <- expand [expand[,2] != expand[,3],]

##  ----------------------------------------------------------------------------  ##
##  dropping equals observations (when the levels of the variable are different)  ##
##  ----------------------------------------------------------------------------  ##

data <- expand
data$ido <- as.character (data$ido)
data$idd <- as.character (data$idd)

df <- subset (data, ido != idd)

##  -----------  ##
##  saving base  ##
##  -----------  ##

setwd ('C:\\Users\\Inspiron\\OneDrive\\quantitative_methods_applied\\thesis_project\\essay\\mercosur\\base')

write.dta (expand, 'expand.dta', convert.factors = 'string')

write.dta (df, 'df.dta', convert.factors = 'string')


##  -----------------------------------------------------  ##
##  -----------------------------------------------------  ##
##  ---  computacional routines for article graphics  ---  ##
##  -----------------------------------------------------  ##
##  -----------------------------------------------------  ##

##  ------------------------------------  ##
##  code for figure 1  code for figure 1  ##
##  ------------------------------------  ##

library(reshape2)
library(ggplot2)

p <- ggplot(data = mydata, aes(x = year, y = acumulated)) + geom_line(size = .8, colour = "blue") + geom_vline(xintercept = 1992, colour = "orange", linetype = "longdash") + xlab("Ano \nFonte: Organização Mundial do Comércio (OMC)") + ylab("Número de acordos em vigor") + geom_text(data = NULL, x = 1996, y = 30, label = "1992", colour = "red")

## ggplot2

ggplot(data = mydata, aes(x = year, y = force, fill = force)) + geom_bar(stat = "identity") + xlab("Ano \nFonte: Organização Mundial do Comércio (OMC)") + ylab("Acordos em vigor")

## area

ggplot(data = mydata, aes(x = year, y = acumulated)) + geom_area(alpha = .5) + geom_vline(xintercept = 1992,size = 1) + theme_bw() + xlab("Ano \nFonte: Organização Mundial do Comércio (OMC)") + ylab("Número de acordos em vigor") + geom_text(data = NULL, x = 1990, y = 38, label = "1992", angle = 90, size = 5)

## sem area

ggplot(data = mydata, aes(x = year, y = acumulated)) + geom_area(alpha = .5) + geom_line(size = .8) + geom_vline(xintercept = 1992, size = 1) + theme_bw() + xlab("Ano \nFonte: Organização Mundial do Comércio (OMC)") + ylab("Número de acordos em vigor") + geom_text(data = NULL, x = 1990, y = 38, label = "1992", angle = 90, size = 5)


##  ------------------------------------  ##
##  code for figure 2  code for figure 2  ##
##  ------------------------------------  ##

library (ggplot2)
library (gridExtra)

## arredondamento dos valores da rate

margens$rate <- round(margens$rate, digits = 1)

## nomeando os paises

levels(margens$country)

levels(margens$country) <- c("Argentina", "Bolívia", "Brasil", "Chile", "Colômbia", "Equador", "México", "Nova Zelândia", "Peru", "Paraguai", "Uruguai", "Venezuela")

##  grafico mostrando valores no eixo (fundo cinza)

margins <- ggplot(data = margens, aes(x = country, y = rate)) + geom_bar(stat = "identity", fill = "grey", position = position_dodge(), colour = "black") + xlab("Países") + ylab("Percentual \nFonte: Base pour l'Analyse du Commerce International (BACI).") + geom_text(aes(label = rate, ymax = rate, hjust = ifelse(sign(rate)>0,1,0)), position = position_dodge(width=1)) + coord_flip()

##  grafico sem mostrar valores no eixo (fundo cinza)

margins <- ggplot(data = margens, aes(x = country, y = rate)) + geom_bar(stat = "identity", fill = "grey", position = position_dodge(), colour = "black") + xlab("Países") + ylab("Percentual \nFonte: Base pour l'Analyse du Commerce International (BACI).") + coord_flip()

##  grafico mostrando valores no eixo (fundo branco)

margins <- ggplot(data = margens, aes(x = country, y = rate)) + geom_bar(stat = "identity", fill = "grey", position = position_dodge(), colour = "black") + theme_bw() + xlab("Países") + ylab("Percentual \nFonte: Base pour l'Analyse du Commerce International (BACI).") + geom_text(aes(label = rate, ymax = rate, hjust = ifelse(sign(rate)>0,1,0)), position = position_dodge(width = 1)) + coord_flip()

##  grafico sem mostrar valores no eixo (fundo branco)

margins <- ggplot(data = margens, aes(x = country, y = rate)) + geom_bar(stat = "identity", fill = "grey", position = position_dodge(), colour = "black") + theme_bw() + xlab("Países") + ylab("Percentual \nFonte: Base pour l'Analyse du Commerce International (BACI).") + coord_flip()


##  ------------------------------------  ##
##  code for figure 3  code for figure 3  ##
##  ------------------------------------  ##

library(reshape2)
library(ggplot2)

##  ano base 1992

datalong <- melt(mydata, id = "year")   ## considerei como ano base 1992

ggplot(datalong, aes(year, value, colour = variable)) + geom_line(size = .8) + xlab("Ano \nFonte: Base pour l'Analyse du Commerce International (BACI).") + ylab("Percentual (base 1992)") + theme(legend.title = element_blank()) + geom_vline(xintercept = 1992, colour = "orange", linetype = "longdash") + geom_text(data = NULL, x = 1993, y = 1.025, label = "1992", colour = "red") + theme(legend.background = element_rect(fill = "gray90", size = .5, linetype = "dotted")) + geom_point(aes(shape = variable), size = 4) + scale_shape_manual(values = c(15, 16, 17, 8))

## ano base 1991

baselong <- melt(data, id = "year")   ## considerei como ano base 1991

ggplot(baselong, aes(year, value, colour = variable)) + geom_line(siz e= .8) + xlab("Ano \nFonte: Base pour l'Analyse du Commerce International (BACI).") + ylab("Percentual (base 1991)") + theme(legend.title = element_blank()) + geom_vline(xintercept = 1991, colour = "orange", linetype = "longdash") + geom_text(data = NULL, x = 1992, y = 1.025, label = "1991", colour = "red") + theme(legend.background = element_rect(fill = "gray90", size = .5,linetype = "dotted")) + geom_point(aes(shape = variable), size = 4) + scale_shape_manual(values = c(15, 16, 17, 8))

## ano base 1991 e escala de cinza

baselong<- melt(sur, id = "year")   ## considerei como ano base 1991

## fundo cinza ##

ggplot(baselong, aes(year, value, colour = variable)) + geom_line(size = .8) + xlab("Ano \nFonte: Base pour l'Analyse du Commerce International (BACI).") + ylab("Percentual (base 1991)") + theme(legend.title = element_blank(), legend.position = c(.9, .885), legend.text = element_text(size = 15)) + geom_vline(xintercept = 1991, size = 1) + geom_text(data = NULL, x = 1992, y = 1.025, label = "1991", colour = "black") + theme(legend.background = element_rect(fill = "gray90", size = .5, linetype = "dotted")) + geom_point(aes(shape = variable), size = 4) + scale_shape_manual(values = c(8, 16, 17, 15)) + scale_colour_grey()

## fundo branco ##

ggplot(baselong, aes(year, value, colour = variable)) + geom_line(size = .8) + xlab("Ano \nFonte: Base pour l'Analyse du Commerce International (BACI).") + ylab("Percentual (base 1991)") + theme_bw() + theme(legend.title = element_blank(), legend.position = c(.9, .885), legend.text = element_text(size = 15)) + geom_vline(xintercept = 1991, size = 1) + geom_text(data = NULL, x = 1992, y = 1.025, label = "1991", colour = "black") + theme(legend.background = element_rect(size = .5, linetype = "dotted")) + geom_point(aes(shape = variable), size = 4) + scale_shape_manual(values = c(8, 16, 17, 15)) + scale_colour_grey()
